package com.earl.carnet.concurrent.test;

import org.junit.runner.notification.RunListener.ThreadSafe;

@ThreadSafe
public class wrongsingleton {
    private static volatile wrongsingleton _instance = null; 
 
    private wrongsingleton() {}
 
    public static wrongsingleton getInstance() {
 
        if (_instance == null) {
            _instance = new wrongsingleton();
            System.out.println("--initialized once.");
        }
        
        return _instance;
    }
    
    private static class LoopInit implements Runnable {
        public void run() {
            long val = 0;
            while (val < 100000L) {
                getInstance();
                val++;
            }
        }
    }
    private static class LoopInit2 implements Runnable {
    	public void run() {
    		long val = 0;
    		while (val < 100000L) {
    			getInstance();
    			val++;
    		}
    	}
    }
    
    public static void main(String[] args){
        
        Thread t1 = new Thread(new LoopInit());
        Thread t2 = new Thread(new LoopInit2());
        Thread t3 = new Thread(new LoopInit());
        Thread t4 = new Thread(new LoopInit2());
        Thread t5 = new Thread(new LoopInit());
        Thread t6 = new Thread(new LoopInit2());
        Thread t7 = new Thread(new LoopInit());
        Thread t8 = new Thread(new LoopInit2());
        Thread t9 = new Thread(new LoopInit());
        Thread t10 = new Thread(new LoopInit2());
        t1.start();
        t2.start();
        t3.start();
        t4.start();
        t5.start();
        t6.start();
        t7.start();
        t8.start();
        t9.start();
        t10.start();
         
        while (t1.isAlive() || t2.isAlive() || t3.isAlive()|| t4.isAlive() || t5.isAlive() ||t6.isAlive() || t7.isAlive() || t8.isAlive()|| t9.isAlive()||t10.isAlive()) {
             
        }
 
    }
}
 
